package com.fp.helig.SceneControler;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.camera.SmoothCamera;
import org.anddev.andengine.entity.IEntity;
import org.anddev.andengine.entity.modifier.MoveModifier;
import org.anddev.andengine.entity.sprite.AnimatedSprite;
import org.anddev.andengine.extension.physics.box2d.PhysicsConnector;
import org.anddev.andengine.extension.physics.box2d.PhysicsFactory;
import org.anddev.andengine.extension.physics.box2d.PhysicsWorld;
import org.anddev.andengine.extension.physics.box2d.util.constants.PhysicsConstants;
import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;
import org.anddev.andengine.ui.activity.BaseGameActivity;

import android.R.bool;
import android.util.Log;

import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.fp.helig.Res.ObjectParams;
import com.fp.helig.Res.Textures;

public class DynamicObject extends BaseObject{


	public Body mBody;
	
	public DynamicObject(float pX, float pY,ObjectParams pParams, PhysicsWorld physicsWorld, SmoothCamera camera) {
		// TODO Auto-generated constructor stub 
		 super(pX, pY,pParams, physicsWorld,camera);
		
		 createPhysics(camera,physicsWorld);
			//this.animate(55);
		 	//this.setRotation(0.0f);
	}
	
	
	private void createPhysics(final SmoothCamera camera, PhysicsWorld physicsWorld)
	{	
		
		switch (mParams.mBodyType) {
		case BOX_BODY:
			mBody = PhysicsFactory.createBoxBody(physicsWorld, this, BodyType.DynamicBody, 
					PhysicsFactory.createFixtureDef(mParams.mDensity,mParams.mElasticity,mParams.mFriction));			
			break;
		case CIRCLE_BODY:
			mBody = PhysicsFactory.createCircleBody(physicsWorld, this, BodyType.DynamicBody, 
					PhysicsFactory.createFixtureDef(mParams.mDensity,mParams.mElasticity,mParams.mFriction));
			break;
		default:
			
			break;
		}
		mBody.setUserData(super.returnThis());

		physicsWorld.registerPhysicsConnector(new PhysicsConnector(this, mBody, true, true)
		{
			@Override
	        public void onUpdate(float pSecondsElapsed)
	        {
				super.onUpdate(pSecondsElapsed);
				if (mParams.mName.equals("Player")){
					//camera.onUpdate(0.1f);
					boolean moveCam=false;
					float boxSizeX=camera.getWidth()/2.f-2.55f*getWidth()/4;
					float boxSizeY=camera.getHeight()/2.f-2.55f*getHeight()/4;
					if ((camera.getCenterX()+boxSizeX)<=(getX()+getWidth()/2)){
					//	camera.setCenter(getX(), getY());
						moveCam=true;
					}
					
					if ((camera.getCenterX()-boxSizeX)>=(getX()+getWidth()/2)){
						//camera.setCenter(getX(), getY());
						moveCam=true;
					}
					
					if ((camera.getCenterY()+boxSizeY)<=(getY()+getHeight()/2)){
					//	camera.setCenter(getX(), getY());
						moveCam=true;
					}
					
					if ((camera.getCenterY()-boxSizeY)>=(getY()+getHeight()/2)){
					//	camera.setCenter(getX(), getY());
						moveCam=true;
					}
					
					if(moveCam){
						float pixelsInMeters = PhysicsConstants.PIXEL_TO_METER_RATIO_DEFAULT;
						//Log.v(String.valueOf(this.getBody().getLinearVelocity().x), String.valueOf(this.getBody().getLinearVelocity().y));
						camera.setMaxVelocity(1.0f*Math.abs(this.getBody().getLinearVelocity().x)*pixelsInMeters,1.0f*Math.abs(this.getBody().getLinearVelocity().y)*pixelsInMeters);
						camera.setCenter(getX()+getWidth()/2+this.getBody().getLinearVelocity().x*pixelsInMeters*3*pSecondsElapsed, getY()+getHeight()/2+this.getBody().getLinearVelocity().y*pixelsInMeters*3*pSecondsElapsed);
						
					}
					
				}
				
	        }
		});
	}

	
	public void Action(int id){
		if (curentAction!=id){
			switch (id) {
			case ACTION_JUMP:
				
				
				break;
			case ACTION_STOP:
				
				break;
			default:
				
				break;
			}
			curentAction=id;
		}
		
	}
}
